package com.iwhalecloud.bss.kite.cucc.service.attr;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.ILocalQueryScenePersonInfoService;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service("localQueryScenePersonService")
public class LocalQueryScenePersonService implements ILocalQueryScenePersonInfoService {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public DataResult<List<InstAttrDTO>> listKiteConfigAttrs(String sceneInstId, String objType) {
        List<KiteAttr> personAttrs = listAttrs(sceneInstId, objType);
        return ErrorConsts.SUCCESS.getResult(CopyToDtoUtil.transInstAttrDtoList(personAttrs));
    }

    @Override
    public DataResult<List<InstAttrDTO>> listAllKiteConfigAttrs(String sceneInstId, String custId) {
        List<KiteAttr> commonAttrs = listAttrsByModuleId(sceneInstId, LocalKeyConsts.CONTACTINFO_MODULEID);
        commonAttrs.addAll(listAttrsByModuleId(sceneInstId, LocalKeyConsts.DEVPERSONINFO_MODULEID));
        commonAttrs.addAll(listAttrsByModuleId(sceneInstId, LocalKeyConsts.AGENTINFO_MODULEID));
        commonAttrs.addAll(listAttrsByModuleId(sceneInstId, LocalKeyConsts.HANDLERCUSTINFO_MODULEID));
        commonAttrs.addAll(listAttrsByModuleId(sceneInstId, LocalKeyConsts.CUSTMANAGERINFO_MODULEID));
        commonAttrs.addAll(listAttrsByModuleId(sceneInstId, LocalKeyConsts.GROUPUSERINFO_MODULEID));
        return ErrorConsts.SUCCESS.getResult(CopyToDtoUtil.transInstAttrDtoList(commonAttrs));
    }

    @Override
    public DataResult<List<InstAttrDTO>> listKiteConfigAttrsByModuleId(String sceneInstId, String moduleId) {
        List<KiteAttr> commonAttrs = listAttrsByModuleId(sceneInstId, moduleId);
        return ErrorConsts.SUCCESS.getResult(CopyToDtoUtil.transInstAttrDtoList(commonAttrs));
    }

    @Override
    public DataResult<List<InstAttrDTO>> listContractInfoAttrs(String sceneInstId) {
        return listKiteConfigAttrs(sceneInstId, LocalKeyConsts.CONTACT_OBJECT_TYPE);
    }

    @Override
    public DataResult<List<InstAttrDTO>> listDevPersonInfoAttrs(String sceneInstId) {
        return listKiteConfigAttrs(sceneInstId, LocalKeyConsts.DEVPERSON_OBJECT_TYPE);
    }

    @Override
    public DataResult<List<InstAttrDTO>> listAgentPersonInfoAttrs(String sceneInstId) {
        return listKiteConfigAttrs(sceneInstId, LocalKeyConsts.AGENTPERSON_OBJECT_TYPE);
    }

    @Override
    public DataResult<List<InstAttrDTO>> listHandlerCustInfoAttrs(String sceneInstId) {
        return listKiteConfigAttrs(sceneInstId, LocalKeyConsts.HANDLERCUST_OBJECT_TYPE);
    }

    @Override
    public DataResult<List<InstAttrDTO>> listCustInfoAttrs(String sceneInstId) {
        return listKiteConfigAttrs(sceneInstId, LocalKeyConsts.CUSTINFO_OBJECT_TYPE);
    }

    @Override
    public DataResult<List<InstAttrDTO>> listGoodsDisplayInfoAttrs(String sceneInstId) {
        return listKiteConfigAttrs(sceneInstId, LocalKeyConsts.GOODS_DISPLAY_OBJECT_TYPE);
    }

    public List<KiteAttr> listAttrs(String sceneInstId, String objType) {
        List<KiteAttr> personAttrs = querySceneInstService.listCommonAttrs(sceneInstId, objType);
        return Optional.ofNullable(personAttrs).orElse(new ArrayList<>());
    }

    public List<KiteAttr> listAttrsByModuleId(String sceneInstId, String moduleId) {
        List<KiteAttr> personAttrs = querySceneInstService.listCommonAttrsByModuleId(sceneInstId, moduleId);
        return Optional.ofNullable(personAttrs).orElse(new ArrayList<>());
    }

}
